package com.gsoe.problem.intercept;

import com.gsoe.problem.po.Exam;
import com.gsoe.problem.service.ExamService;
import com.gsoe.problem.utils.BloomFilterUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author CoderJia
 * @create 2024/10/6 下午 12:38
 * @Description
 **/
@Slf4j
@Component
public class BloomFilterInitializer implements CommandLineRunner {

    @Autowired
    private BloomFilterUtil bloomFilterService;

    @Autowired
    private ExamService examService;

    @Override
    public void run(String... args) throws Exception {

        // 查询所有产品数据
        List<Long> all = examService.list().stream().map(Exam::getId).toList();

        // 初始化布隆过滤器
        bloomFilterService.initBloomFilter(all.size(), 0.01);

        // 将所有产品的ID加入布隆过滤器
        all.forEach(product -> {
            bloomFilterService.addToBloomFilter(product.toString());
        });
        log.info("初始化布隆过滤器完成，添加产品数：{}", all.size());
    }
}
